package com.example.module_mine.mine

import android.view.View
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.Router
import com.example.lib_common.base.BaseFragment
import com.example.lib_common.event.EventType
import com.example.lib_common.event.LiveDataBus
import com.example.lib_common.event.LoginEvent
import com.example.lib_storage.AuthMMKV
import com.example.module_mine.R
import com.example.module_mine.databinding.FragmentMineBinding
import com.example.module_mine.mine.adapter.ToolAdapter
import com.example.module_mine.mine.model.TabItem
import com.example.module_mine.mine.viewmodel.MineViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 我的页面：登陆状态/未登陆状态
 * 登陆注册。。。。。。
 * 事件总线
 */
@Route(path = Router.MINE_MINE_FRAGMENT)
@AndroidEntryPoint
class MineFragment : BaseFragment<MineViewModel,FragmentMineBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_mine
    override val mViewModel: MineViewModel by activityViewModels()

    companion object {
        const val CLUB_MGR = "club_mgr"
        const val DVR = "dvr"
        const val GROUP = "group"
        const val MSG_CENTER = "msg_center"
        const val STOLEN = "stolen"
        const val APP_SHARE = "share"
        const val HELP_CENTER = "help_center"
        const val WIKI = "wiki"
        const val CUSTOMER = "customer"
        const val JOIN = "join"
        const val SETTINGS = "settings"

        const val STORE_SEARCH = "store_search"
        const val BOOKING_DRV = "booking_drv"
        const val RESCUE = "rescue"
        const val CARE_SERVICE = "care_service"
        const val FIX_ONLINE = "fix_online"

        const val MALL_ORDER = "store_search"
        const val MALL_FAVORITES = "mall_favorites"
        const val MALL_COUPON = "mall_coupon"
        const val CYCLONE_BEAN = "cyclone_bean"

        const val CHAT = "chat"
        const val ROUTE = "route"


        const val MY_CAR = "my_car"
        const val DRIVING_DATA = "driving_data"
        const val DRIVING_GROUP = "driving_group"
        const val MY_ACTIVITY = "my_activity"
    }
    //数据源
    private val mMallTabItem by lazy{
        listOf(
            TabItem(MALL_ORDER,"订单",R.mipmap.icon_mall_order,false),
            TabItem(MALL_COUPON,"优惠券",R.mipmap.icon_mall_coupon,false),
            TabItem(CYCLONE_BEAN,"龙豆兑换",R.mipmap.icon_cyclone_bean,false)
        )
    }
    private val mMyCarTabItem by lazy{
        listOf(
            TabItem(MY_CAR, "添加车辆", R.mipmap.icon_add_motor, false),
            //  TabItem(DRIVING_DATA, "历史骑行", R.mipmap.icon_driving_data, false)
            TabItem(MY_ACTIVITY, "我的活动", R.mipmap.icon_my_activity, false),
            TabItem(DRIVING_GROUP, "骑行组队", R.mipmap.icon_drive_group, false),
        )
    }
    private val mServiceTabItem by lazy{
        listOf(
            TabItem(STORE_SEARCH, "寻找门店", R.mipmap.icon_search_store, false),
            TabItem(BOOKING_DRV, "预约试驾", R.mipmap.icon_booking, false),
            TabItem(RESCUE, "一键救援", R.mipmap.icon_rescue, false),
            TabItem(CARE_SERVICE, "保养服务", R.mipmap.icon_care_service, false),
            TabItem(FIX_ONLINE, "在线报修", R.mipmap.icon_fix_online, false),
        )
    }
    private val mToolTabItem by lazy{
        listOf(
            TabItem(CLUB_MGR, getString(com.example.lib_common.R.string.string_club_management), R.mipmap.icon_club_management, false),
            TabItem(WIKI, getString(com.example.lib_common.R.string.string_menu_wiki), R.mipmap.icon_menu_wiki, false),
            // TabItem(MSG_CENTER, getString(R.string.string_message_centre), R.mipmap.icon_mine_message_centra, true),
            TabItem(STOLEN, getString(com.example.lib_common.R.string.string_stolen_applications), R.mipmap.icon_stolen_applications, false),
            TabItem(APP_SHARE, getString(com.example.lib_common.R.string.string_app_share), R.mipmap.icon_app_share, false),
            TabItem(HELP_CENTER, getString(com.example.lib_common.R.string.string_help_center), R.mipmap.icon_help, false),
            TabItem(CUSTOMER, getString(com.example.lib_common.R.string.string_contact_customer), R.mipmap.icon_customer, false),
            TabItem(JOIN, getString(com.example.lib_common.R.string.string_commercial_join), R.mipmap.icon_commercial_join, false),
            TabItem(SETTINGS, getString(com.example.lib_common.R.string.string_settings_center), R.mipmap.icon_settings, false),
        )
    }

    //适配器
    private val mMallAdapter by lazy{//by lazy 什么时候调用什么初始化
        ToolAdapter(mMallTabItem){
            Toast.makeText(requireContext(),"点击了菜单 ${it.tabName}",Toast.LENGTH_SHORT).show()
        }
    }
    private val mMyCarAdapter by lazy{//by lazy 什么时候调用什么初始化
        ToolAdapter(mMyCarTabItem){
            Toast.makeText(requireContext(),"点击了菜单 ${it.tabName}",Toast.LENGTH_SHORT).show()
        }
    }

    private val mServiceAdapter by lazy{//by lazy 什么时候调用什么初始化
        ToolAdapter(mServiceTabItem){
            Toast.makeText(requireContext(),"点击了菜单 ${it.tabName}",Toast.LENGTH_SHORT).show()
        }
    }
    private val mToolAdapter by lazy{//by lazy 什么时候调用什么初始化
        ToolAdapter(mToolTabItem){
            Toast.makeText(requireContext(),"点击了菜单 ${it.tabName}",Toast.LENGTH_SHORT).show()
        }
    }

    override fun initView() {
        initShowView()
        mBinding.rvMall.adapter = mMallAdapter
        mBinding.rvMyCar.adapter = mMyCarAdapter
        mBinding.rvService.adapter = mServiceAdapter
        mBinding.rvTool.adapter = mToolAdapter
    }

    private fun initShowView(){
        if(AuthMMKV.token.isNullOrEmpty()){//未登陆状态
            onNotLogin()
        }else{//登陆状态
            onLogin()
        }
    }

    private fun onNotLogin() {
        mBinding.lUserInfo.visibility = View.GONE
        mBinding.btnLogin.visibility = View.VISIBLE
        mBinding.mineModel = null
    }
    private fun onLogin(){
        mBinding.lUserInfo.visibility = View.VISIBLE
        mBinding.btnLogin.visibility = View.GONE
//        //网络请求我的用户信息展示
//        mBinding.mineModel = null
    }

    override fun initData() {
        //获得用户信息并展示
        if(AuthMMKV.token.isNotEmpty()){
            mViewModel.getUserInfo()
        }

    }

    override fun initListener() {
        //点击跳转登陆页面
        mBinding.btnLogin.setOnClickListener {
            ARouter.getInstance().build(Router.MINE_LOGIN_ACTIVITY).navigation()
        }
        //订阅登陆成功事件
        LiveDataBus.with<LoginEvent>(EventType.LOGIN).observe(this){
            onLogin()
            initData()
        }
        //订阅用户信息
        mViewModel._userInfo.observe(this){
            mBinding.mineModel = it
            mBinding.executePendingBindings() //刷新界面
        }
    }




}